﻿using System;
using System.Reflection;

namespace Hxsoft.AppV1.Module
{
	/// <summary>
	/// 系统初始化。
	/// </summary>
	public partial class SystemInit
	{
		public static SystemInit Instance
		{
			get
			{
				string assemblyName = ApplicationConfiguration.Configuration.SystemInit.SystemInitAssembly;
				string className = ApplicationConfiguration.Configuration.SystemInit.SystemInitClassName;
				if (string.IsNullOrEmpty(assemblyName)) assemblyName = "Hxsoft.AppV1.Module";
				if (string.IsNullOrEmpty(className)) className = "Hxsoft.AppV1.Module.ExtSystemInit";

				Assembly assembly = Assembly.Load(assemblyName);
				return (SystemInit)assembly.CreateInstance(className);
			}
		}

		/// <summary>
		/// 初始化权限。
		/// </summary>
		public virtual void InitPermissions()
		{
			// 权限验证
			GetInitRight(true);

			RegisterPermissions();
			Articles.RegisterPermissions();
			Comments.RegisterPermissions();
			DicClasses.RegisterPermissions();
			InfoItems.RegisterPermissions();
			InfoResources.RegisterPermissions();
			LinkClasses.RegisterPermissions();
			MessageBoards.RegisterPermissions();
			Modules.RegisterPermissions();
			PageBlocks.RegisterPermissions();
			Roles.RegisterPermissions();
			Sites.RegisterPermissions();
			SqlCacheDependency.RegisterPermissions();
			Users.RegisterPermissions();
			VoteClasses.RegisterPermissions();
			OverallControl.RegisterPermissions();
			Specials.RegisterPermissions();
			FeedBacks.RegisterPermissions();
            KeywordForLinks.RegisterPermissions();
			CachePages.RegisterPermissions();
			TeamBlacks.RegisterPermissions();
		}

		/// <summary>
		/// 初始化角色。
		/// </summary>
		public static void InitRoles()
		{
			// 权限验证
			GetInitRight(true);

			Roles.RegisterAdministratorRole();

			Roles.RegisterRole(new Role
			{
				RoleName = Roles.EveryoneRoleName,
				Description = "所有人",
				PropertyCode = (int)(Roles.RolePropertyCode.DisallowDelete | Roles.RolePropertyCode.DisallowUserAccredit)
			});

			Roles.RegisterRole(new Role
			{
				RoleName = Roles.CreatorRoleName,
				Description = "创建者",
				PropertyCode = (int)(Roles.RolePropertyCode.DisallowDelete | Roles.RolePropertyCode.DisallowUserAccredit)
			});

			Roles.RegisterRole(new Role
			{
				RoleName = Roles.OwnerRoleName,
				Description = "所有者",
				PropertyCode = (int)(Roles.RolePropertyCode.DisallowDelete | Roles.RolePropertyCode.DisallowUserAccredit)
			});

			Roles.RegisterRole(new Role
			{
				RoleName = Roles.UserRoleName,
				Description = "用户",
				PropertyCode = (int)(Roles.RolePropertyCode.DisallowDelete | Roles.RolePropertyCode.DisallowUserAccredit)
			});

			Roles.RegisterRole(new Role
			{
				RoleName = "CompanyGrade1",
				Description = "初级会员",
				PropertyCode = (int)(Roles.RolePropertyCode.DisallowDelete | Roles.RolePropertyCode.DisallowUserAccredit)
			});

			Roles.RegisterRole(new Role
			{
				RoleName = "CompanyGrade2",
				Description = "中级会员",
				PropertyCode = (int)(Roles.RolePropertyCode.DisallowDelete | Roles.RolePropertyCode.DisallowUserAccredit)
			});

			Roles.RegisterRole(new Role
			{
				RoleName = "CompanyGrade3",
				Description = "高级会员",
				PropertyCode = (int)(Roles.RolePropertyCode.DisallowDelete | Roles.RolePropertyCode.DisallowUserAccredit)
			});

			Roles.RegisterRole(new Role
			{
				RoleName = "CompanyUser",
				Description = "企业用户",
				PropertyCode = (int)(Roles.RolePropertyCode.DisallowDelete | Roles.RolePropertyCode.DisallowUserAccredit)
			});

			Roles.RegisterRole(new Role
			{
				RoleName = "CompanyManager",
				Description = "企业管理员",
				PropertyCode = (int)(Roles.RolePropertyCode.DisallowDelete | Roles.RolePropertyCode.DisallowUserAccredit)
			});

			Roles.RegisterRole(new Role
			{
				RoleName = "OwnedCompanyUser",
				Description = "所属企业用户",
				PropertyCode = (int)(Roles.RolePropertyCode.DisallowDelete | Roles.RolePropertyCode.DisallowUserAccredit)
			});

			Roles.RegisterRole(new Role
			{
				RoleName = "OwnedCompanyManager",
				Description = "所属企业管理员",
				PropertyCode = (int)(Roles.RolePropertyCode.DisallowDelete | Roles.RolePropertyCode.DisallowUserAccredit)
			});
		}

		/// <summary>
		/// 初始化用户。
		/// </summary>
		public static void InitUsers()
		{
			// 权限验证
			GetInitRight(true);

			_InitUsers();
		}

		/// <summary>
		/// 初始化用户。
		/// </summary>
		internal static void _InitUsers()
		{
			Users.RegisterAdministratorUser();
		}

		/// <summary>
		/// 确保系统已经初始化了管理员用户。
		/// </summary>
		public static void EnsureInitUsers()
		{
			if (Users.Exists(Users.AdministratorUserName)) return;
			_InitUsers();
		}
	}



	/// <summary>
	/// SqlCacheDependency 类对应操作权限部分。
	/// </summary>
	public partial class SystemInit
	{
		#region GetInitRight
		/// <summary>
		/// 获取当前用户是否有初始化系统的权限。
		/// </summary>
		/// <param name="dc">数据容器。</param>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有初始化系统的权限。</returns>
		public static bool GetInitRight(GenericModuleDataContext dc, bool triggerException)
		{
			string userName = Users.CurrentUserName;
			int permissionCode = (int)PermissionCode.Init;
			bool b = dc.AccessController.GetUserIsHasPermission<object>(userName, null, ResourceId, permissionCode);

			if (triggerException && !b)
				throw new PermissionException(userName, null, ResourceId, permissionCode, Res.PermissionExceptionMessage);
			return b;
		}

		/// <summary>
		/// 获取当前用户是否有初始化系统的权限。
		/// </summary>
		/// <param name="triggerException">当未通过权限验证的时候是否引发异常。</param>
		/// <returns>布尔值表示当前用户是否有初始化系统的权限。</returns>
		public static bool GetInitRight(bool triggerException)
		{
			return GetInitRight(new GenericModuleDataContext(), triggerException);
		}

		/// <summary>
		/// 获取当前用户是否有初始化系统的权限。
		/// </summary>
		/// <returns>布尔值表示当前用户是否有初始化系统的权限。</returns>
		public static bool GetInitRight()
		{
			return GetInitRight(false);
		}
		#endregion
	}
}